<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryGoods.query"
            class="input-with-select"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="queryGoodsName"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table :data="goodsList" style="width: 100%" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="700px"
        ></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
        ></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" width="180px">
          <template v-slot="{ row }">
            {{ row.add_time | formatDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template v-slot="{ row }">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeGood(row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryGoods.pagenum"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="queryGoods.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数列表
      queryGoods: {
        query: '',
        pagenum: 1,
        pagesize: 6
      },
      // 商品数据列表
      goodsList: [],
      // 总数据条数
      total: 0
    }
  },
  methods: {
    // 请求商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryGoods
      })
      if (res.meta.status !== 200) {
        return this.$message.error('请求商品列表数据失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(this.goodsList)

      this.$message.success('请求商品列表数据成功')
    },
    // 查询商品
    async queryGoodsName() {
      this.getGoodsList()
    },
    // 当size发生变化的时候触发事件
    handleSizeChange(size) {
      this.queryGoods.pagesize = size
      this.getGoodsList()
    },
    // 当当前页码值发生变化时触发事件
    handleCurrentChange(curr) {
      this.queryGoods.pagenum = curr
      this.getGoodsList()
    },
    // 点击删除商品
    async removeGood(id) {
      // 显示删除商品的提示框
      const result = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      console.log(result)
      if (result !== 'confirm') {
        return this.$message('用户取消了删除操作')
      }
      // 发起删除商品的请求
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败')
      }
      this.$message.success('删除商品成功')
      this.getGoodsList()
    },
    // 跳转至添加商品页面
    goAddPage() {
      this.$router.push('/goods/add')
    }
  },
  created() {
    this.getGoodsList()
  }
}
</script>

<style  lang='less' scoped >
.el-breadcrumb {
  margin-bottom: 20px;
}

.el-row {
  margin-bottom: 20px;
}

.el-pagination {
  margin-top: 10px;
}
</style>
